﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Microsoft.SqlServer.Types;
using System.Data.SqlClient;

namespace SensorNetworkUI
{
    public class NetworkDataStructure
    {
        string name;
        int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        public string Name
        {
            get { return name; }
            set { name = value; }

        }

        List<SensorStructure> sensors = new List<SensorStructure>();

        public List<SensorStructure> Sensors
        {
            get { return sensors; }
            set { sensors = value; }
        }
        public bool PopulateSensors()
        {



            foreach(SensorStructure sens in sensors){

                sens.fetch();
            }

            return true;

        }

        public bool Fetch()
        {
            SqlConnection conn = new SqlConnection(Constants.ConnectionString);
            SqlCommand comm = conn.CreateCommand();
            string query = "select * from network_association where network_id = @id";

            comm.CommandText = query;
            conn.Open();
            comm.Parameters.AddWithValue("@id", this.id);
            SqlDataReader read = comm.ExecuteReader();
            while (read.Read())
            {

                SensorStructure sens = new SensorStructure();
                sens.Id = int.Parse(read["sensor_id"].ToString());
                sensors.Add(sens);

            }
            conn.Close();
          



            return true;


        }

        public double Average
        {

            get
            {
                double ret = 0;
                foreach (SensorStructure sens in sensors)
                {
                    ret += sens.Latesttemp;

                }
                if(sensors.Count != 0 ) 
                    ret = ret / sensors.Count;

                return ret;
            }
        }
    }
 
}